<?php
	
require_once("database.php");
$id = $_GET['id'];
$foreignKey = $_GET['foreignKey'];
$bodyString = "";
if ($id) {
	$SQLreadQuery = "SELECT * FROM best_news WHERE id = $id";
	$SQLreadResult = $db->query($SQLreadQuery);
	while ($rowArray = $db->fetch_array($SQLreadResult)) {
		$sort_order = $rowArray["sort_order"];
		$timestamp = $rowArray["timestamp"];
		$title = $rowArray["title"];
		$publishDate = $rowArray["publishDate"];
		$body = $rowArray["body"];
		$id = $rowArray["id"];
	}						
}

require_once('io_library.php');
if (session_id() != "") {
	//session_start(); 
	//echo "starting a new session now";
}
session_start();
if ($_GET['sessionDepth'] && $_SERVER['HTTP_REFERER']) { // if it needs a ref URL and they have SERVER data
	$sessionDepth =  'depth' . $_GET['sessionDepth'];
	if (!$_SESSION[$_GET['sessionDepth']] || $_SESSION[$sessionDepth] == "") {
		//session_register($sessionDepth);
		$_SESSION[$sessionDepth] = $_SERVER['HTTP_REFERER'];
	}
	$referringPage = $_SESSION[$sessionDepth]; // then give 'em what DEY need
}
//print_r($_SESSION);
	
if (!$referringPage)						// else send to parent 'list' page							
	$referringPage = "best_news_list.php";
?>
<html>
	<head>
		<title>TheBest.com > News</title>
		<?php require_once('io_writePage_includes.php'); ?>
	<script type='text/javascript'>
		var refererURL = '<?php echo $referringPage; ?>';
		var parent_id = '<?php echo $id; ?>';
		var parent_table = 'best_news';
	</script>
	</head>
	<body>
	<div class='navigation'>
	  <div id='goTo'>Menu...</div>
	  <div id="tableList">
    <?php
    $currentNavTable = 'best_news';
    require_once('navlist.php');
    ?>
    </div> <!-- end #tableList -->
	  <div class='siteName'>TheBest.com</div><div id='currentTableTitle'><a href='best_news_list.php'>News</a></div>
	</div> <!-- end .navigation -->
	<div id="fieldsList">
		<form id='best_newsForm' method="post" action="<?php echo $referringPage; ?>"><?php echo "<input class='formElement' type='hidden' name='sort_order' value='$sort_order' />";
			if ($id)
				echo "
		<input type='hidden' class='formElement best_newsInput' name='timestamp' id='timestamp_timeStamp'  value='".strtotime($timestamp)."' />
";
		// Entry Method Type : {textField}
		echo "<label for='title'>Title</label><input type='text' name='title' size='100' class='formElement best_newsInput' value='$title'>";
		// Entry Method Type : {textField}
		echo "<label for='publishDate'>Date / Month Published</label><input type='text' name='publishDate' size='100' class='formElement best_newsInput' value='$publishDate'>";
		// Entry Method Type : {textArea}
		echo "<label for='body'>Body Text</label><textarea name='body' cols='40' rows='6' class='formElement best_newsInput'>$body</textarea>";
	 // Entry Method Type : {linkedTableList}
    $best_news_id = $id;
    $linkTableName = "images_LINK_newsImages";
    $childTableName = "best_newsImages";
    $parentTable_id = "best_news_id";
    $parentTable_SQL_where = "best_news_id = $id";
    $parentTable = "best_news";
    if ($id) { ?>

    <label>Images</label> 
    <script type='text/javascript'>
      var child_tablename = 'best_newsImages',
          child_fieldname = 'title',
          link_tablename = 'images_LINK_newsImages';
      $(function() {
        $('#images_LINK_newsImages .sortable').sortable({ 
          items: '.backListRecord',
          handle: '.dragIcon',
          update: function() {
            var orderString = '';
            var order = $('#images_LINK_newsImages .sortable').sortable('toArray');
            for (i=0;i<order.length;i++) {
              if (i != 0) { 
                orderString = orderString + '&'; 
              }
              orderString = orderString + 'order[]='+order[i];
            }
            console.log(order);
            $.ajax({
              type: 'GET',
              url: 'ajax.php',
              data: 'AJAXtask=linksortorderUpdate&link_tablename=images_LINK_newsImages&parent_recordID='+parent_id+'&tablename=best_newsImages&'+orderString,
              success: function(msg){
                //alert( \'Data Saved: \' + msg );
              }
            });  
          }
        }); // end .sortable()
      }); // end jQuery
    </script>		
<?php
    echo "
    <div class='linkedTable' id='images_LINK_newsImages'>
      <div class='linkedTableList sortable'>";			
      require_once('best_newsImages_linkedtablelist.php');
    echo "
      </div>";
    echo "
<div class='popupLink poplight' id='poplight_1'>Choose from existing items</div>
<!--POPUP START-->
<div id='popup1' class='popup_block'>
  <div class='ajaxDestination'></div>	   
</div>
<!-- POPUP END -->
";
    echo "<a class='newBtn positiveBtns' href='best_newsImages_write.php?foreignKey=".$best_news_id."&sessionDepth=".($_GET['sessionDepth']+1)."&linkedChild=best_newsImages'>NEW RECORD</a>";						
			echo "</div>";} ?>
		<input type="hidden" name="id" value="<?php echo $_GET['id'];?>" />
		<?php 
		if ($_GET['linkedChild']) {
			echo "<input type='hidden' name='linkedChild' value='".$_GET['linkedChild']."' />";
		}
		?>
		<input type="hidden" name="TABLENAME" value="best_news" />
		<input type="submit" value="Save" id='saveRecord' />
		</form>
		<input type="submit" value="Cancel" id='cancelRecord' />
	</div>
	</body>
</html>